#include "fs_struct.h"

/*
**	、主要用于管理快设备的相关资源
*/

static struct blockdev_t kernel_blockdev_list[MAX_BLKDEV];
static volatile unsigned char blockdev_index = 0;

/*
**	、当块设备驱动注册块设备到系统时候，会在/dev/xxx下创建major为14，minor不同的block设备
**	、在内核中就会将这个驱动的相关fops以及信息放到内核的一个全局数组下
*/
const struct blockdev_t *get_blkfops(unsigned int major){
	if(major >= MAX_BLKDEV)
		return NULL;
	if(!kernel_blockdev_list[major].fops)
		return NULL;
	return &kernel_blockdev_list[major];
}

/*
**	、support for storage device use
*/
struct blockdev_t *alloc_blkdev(void){
	struct blockdev_t *bdev = kmalloc(struct blockdev_t);
	if(!bdev){
		printk("alloc blockdevice fail\r");
		return NULL;
	}
	return bdev;
}

void free_blkdev(struct blockdev_t *dev){
	if(!dev)
		return;
	kfree(dev);
}


unsigned int register_blkdev(struct blockdev_t *dev){
	struct blockdev_t *tblk = &kernel_blockdev_list[blockdev_index++];
	if(!dev)
		return -ENODATA;
	if(blockdev_index >= MAX_BLKDEV)
		return -ENODEV;
	kmemcpy(tblk->name,dev->name,strlen(dev->name));
	tblk->fops = dev->fops;
	
	return 0;
}


void unregister_blkdev(struct blockdev_t *dev){
	if(!dev)
		return -ENODATA;
	blockdev_index--;
	
	return 0;
}


